//
//  CCMBaseViewController.swift
//  CCMoney_swift
//
//  Created by JKHuang on 2018/11/22.
//  Copyright © 2018 JKHuang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit

class CCMBaseViewController: UIViewController {

    var hiddenCustomBack:Bool = false {
        didSet{
            if !hiddenCustomBack {
                navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back_icon"), style: .plain, target: self, action: #selector(backAction))
            }
            else {
                navigationItem.leftBarButtonItem = nil
            }
        }
    }
    
    var disposeBag = DisposeBag()
    
    //sentMessage 会在调用方法前发送值。
    //methodInvoked 会在调用方法后发送值。
    
    init() {
        super.init(nibName: nil, bundle: nil)
        self.rx
            .methodInvoked(#selector(viewDidLoad)).subscribe(onNext:{ _ in
                self.bindViewModel()
            })
            .disposed(by: disposeBag)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = MainColor
        hiddenCustomBack = false
        // Do any additional setup after loading the view.
    }
    
    public func bindViewModel() {
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension CCMBaseViewController {
    
    @objc func backAction() {
        
        if (presentingViewController != nil) {
            
            if ((navigationController?.viewControllers.count)! <= 1) {
                dismiss(animated: true, completion: nil)
            }
            else {
                navigationController?.popViewController(animated: true)
            }
            
        }else{
            
            navigationController?.popViewController(animated: true)
        }
    }
}
